package com.minibobbo.utilities.spe.actionmodules;

import com.minibobbo.utilities.spe.SimplePlatformEngine;
import com.minibobbo.utilities.spe.SpeDynamicObject;

public abstract class SpeActionModule {
	SimplePlatformEngine e;
	SpeDynamicObject parent;

	private float cooldown =0;
	private float delay;
	
	private int button = -1;
	
	public void init(SimplePlatformEngine engine, SpeDynamicObject parent) {
		e = engine;
		this.parent = parent;

	}

	/**
	 * Sets the button that should be assigned to this action
	 * @param button The button to be assigned.
	 * @return This object for chaining.  NOTE: CALL OBJECT SPECIFIC ACTIONS BEFORE THIS ONE.
	 */
	public SpeActionModule setButton(int button) {
		this.button = button;
		return this;
	}
	
	/**
	 * Does the action if the right button is pressed and the cooldown period has expired.
	 * @param delta The amount of time elapsed since the last update.
	 */
	public void takeAction(float delta) {
		delay -= delta;

		if(parent.b[button] && delay <=0) {
			doAction(delta);
			delay = cooldown;
		}
	}
	
	/**
	 * Gets the remaining cooldown time.
	 * @return Cooldown time remaining in seconds.
	 */
	public float getCooldownRemaining() {
		return delay;
	}
	
	/**
	 * Sets the cooldown of this action.  
	 * @param cooldown The length of time before this action can be used again.
	 * @return This object for chaining.  NOTE: CALL OBJECT SPECIFIC ACTIONS BEFORE THIS ONE.
	 */
	public SpeActionModule setCooldown(float cooldown) {
		this.cooldown = cooldown;
		return this;
	}

	/**
	 * Does the action assigned to this button.  Do not call this.  Use takeAction() instead.
	 * @param delta Elapsed time since the last call.
	 */
	protected abstract void doAction(float delta);
}
